package com.anta.it.hr.devtools.webmvc.request.impl;

import com.anta.it.hr.devtools.core.request.impl.AbstractRequestInetAddress;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 普通的http请求，如tomcat，undertow
 * @author wangxiaoxian
 * @since 2021/8/4
 */
public class WebMvcRequestInetAddress extends AbstractRequestInetAddress {

    public String getIpFromHeader(String key, Object loadBalanceKey) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        }
        return requestAttributes.getRequest().getHeader(key);
    }

    public String getRemoteIp(Object loadBalanceKey) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        }
        return requestAttributes.getRequest().getRemoteAddr();
    }
}
